<?php

namespace App\Http\Controllers\Car;


use App\Models\CarModels\CarInfoPic;
use Illuminate\Database\QueryException;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Validator;
/**
 * @group 运营管理->车辆基础信息管理->车辆信息编辑->图片接口
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class CarInfoPicController extends Controller
{
    /**
     * 车辆信息发票等图片添加 删除图片请通过车辆信息修改接口把url设置成null
     * 请使用post  form-data方式传输图片
     *
     * @bodyParam pk int required 要操作图片的对应车辆的id
     * @bodyParam invoice file optional 发票图片url
     * @bodyParam quality file optional 合格证图片url
     * @bodyParam vehicle_license file optional 行驶证图片url
     * @bodyParam registration file optional 登记证图片url
     * @bodyParam maintenance file optional 保养图片url
     *
     */
    public function store(Request $request){
        $e = genValidate();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json(encodeMsg($validator->errors()->messages(),400),400);
        }

        try{
            $pk = Request()->input('pk');
            $car=CarInfoPic::find($pk);
            if($car == null){
                return response()->json(encodeMsg(["message"=>"未找到该车辆"],404),404);
            }
            if($request->file('invoice')){

                //$car->uploadImage(request()->file('invoice'), 'invoice');
                $file = $request->file('invoice');
                $ext = $file->getClientOriginalExtension();
                $realPath = $file->getRealPath();
                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
                Storage::disk('local')->put($filename, file_get_contents($realPath)) && $car->invoice = $filename;
            }

            if($request->file('quality')){
                //$car->uploadImage(request()->file('quality'), 'quality');
                $file = $request->file('quality');
                $ext = $file->getClientOriginalExtension();
                $realPath = $file->getRealPath();
                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
                Storage::disk('local')->put($filename, file_get_contents($realPath)) && $car->quality = $filename;
            }

            if($request->file('vehicle_license')){
                //$car->uploadImage(request()->file('vehicle_license'), 'vehicle_license');

                $file = $request->file('vehicle_license');
                $ext = $file->getClientOriginalExtension();
                $realPath = $file->getRealPath();
                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
                Storage::disk('local')->put($filename, file_get_contents($realPath)) && $car->vehicle_license = $filename;

            }

            if($request->file('registration')){
                //$car->uploadImage(request()->file('registration'), 'registration');

                $file = $request->file('registration');
                $ext = $file->getClientOriginalExtension();
                $realPath = $file->getRealPath();
                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
                Storage::disk('local')->put($filename, file_get_contents($realPath)) && $car->registration = $filename;
            }

            if($request->file('maintenance')){
                //$car->uploadImage(request()->file('maintenance'), 'maintenance');
                $file = $request->file('maintenance');
                $ext = $file->getClientOriginalExtension();
                $realPath = $file->getRealPath();
                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
                Storage::disk('local')->put($filename, file_get_contents($realPath)) && $car->maintenance = $filename;
            }
            return response()->json(encodeMsg($car,201),201);
        }catch (QueryException $e){
            return response()->json(encodeMsg(["message"=>"添加图片失败"],400),400);
        }
    }
}
